import 'package:dio/dio.dart';
import 'package:fsharing/core/services/auth.dart';
import 'package:fsharing/core/viewmodels/view_model.dart';

/// 验证码获取
class ReqCodeModel extends ViewModel {
  static const USER_REGISTER = 'USER_REGISTER';
  static const RESET_PASSWORD = 'RESET_PASSWORD';
  static const REAL_NAME_AUTH = 'REAL_NAME_AUTH';
  static const UPDATE_MOBILE = 'UPDATE_MOBILE';
  static const CONTACT_MOBILE = 'CONTACT_MOBILE';

  final Auth auth;
  String _errorMessage;

  ReqCodeModel(this.auth) : super();

  String get errorMessage => _errorMessage;

  /// USER_REGISTER
  /// RESET_PASSWORD
  /// REAL_NAME_AUTH
  /// UPDATE_MOBILE
  /// CONTACT_MOBILE
  Future<ViewState> reqCode(String type, String phone) async {
    viewState = ViewState.busy;
    try {
      if (phone == null) {
        throw '无手机号码';
      }
      await auth.reqCode(type, phone);
      viewState = ViewState.success;
    } catch (e) {
      _errorMessage = e is DioError ? e.message : e.toString();
      viewState = ViewState.error;
    }
    return viewState;
  }
}
